// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.datainterface.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.R;
import com.fowo.api.datainterface.entity.DatainterfaceAuthority;
import com.fowo.api.datainterface.model.DatainterfaceAuthoritySearchParamPo;
import com.fowo.api.datainterface.model.DatainterfaceAuthorityVo;
import com.fowo.api.datainterface.service.DatainterfaceAuthorityService;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@Tag(name = "接口认证")
@RequestMapping("/api/datainterface-authority")
@RestController
public class DatainterfaceAuthorityController {

    @Resource
    private DatainterfaceAuthorityService service;

    @Operation(summary = "查询结果")
    @GetMapping
    public R<IPage<DatainterfaceAuthority>> search(
            DatainterfaceAuthoritySearchParamPo search
    ) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo != null) {
//            search.setCurrentUserId(jwtUserInfo.getUserId());
        }
        return R.data(service.pageSearch(search));
    }

    @Operation(summary = "查询最后更新时间")
    @GetMapping("lastUpdateTime")
    public R<Date> searchLastUpdateTime(
            DatainterfaceAuthoritySearchParamPo search
    ) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo != null) {
//            search.setCurrentUserId(jwtUserInfo.getUserId());
        }
        return R.data(service.searchLastUpdateTime(search));
    }

    @Operation(summary = "获取分页查询结果")
    @GetMapping("page")
    public R<IPage<DatainterfaceAuthority>> getPage(
            DatainterfaceAuthoritySearchParamPo search
    ) {
        JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
        if (jwtUserInfo != null) {
//            search.setCurrentUserId(jwtUserInfo.getUserId());
        }
        return R.data(service.pageSearch(search));
    }

    @Operation(summary = "获取详情")
    @GetMapping("{id}")
    public R<DatainterfaceAuthorityVo> getVoById(@PathVariable Long id) {
        return R.data(() -> service.getVoById(id));
    }


    @PreAuthorize("@am.canAccess('datainterfaceAuthorityCreate')")
    @Operation(summary = "新增")
    @PostMapping
    public R<Long> create(
            @Validated @RequestBody DatainterfaceAuthorityVo model
    ) {
        return R.data(() -> service.create(model));
    }

    @PreAuthorize("@am.canAccess('datainterfaceAuthorityEdit')")
    @Operation(summary = "更新")
    @PutMapping
    public R update(@RequestBody DatainterfaceAuthorityVo model) {
        return R.run(() -> service.update(model));
    }

    @PreAuthorize("@am.canAccess('datainterfaceAuthorityEdit')")
    @Operation(summary = "编辑更新")
    @PutMapping("edit")
    public R updateForEdit(@RequestBody DatainterfaceAuthorityVo model) {
        return R.run(() -> service.updateForEdit(model));
    }

    @PreAuthorize("@am.canAccess('datainterfaceAuthorityDelete')")
    @Operation(summary = "删除")
    @DeleteMapping("{id}")
    public R delete(@PathVariable Long id) {
        return R.run(() -> service.delete(id));
    }

    @PreAuthorize("@am.canAccess('datainterfaceAuthorityDelete')")
    @Operation(summary = "批量删除")
    @DeleteMapping("batch")
    public R batchDelete(@RequestBody IdListPo po) {
        return R.run(() -> service.batchDelete(po.getIds()));
    }


}
